package returngoods

import (
	// "net/http"
	// "encoding/json"
	"strconv"
	// "time"
	// "github.com/dgrijalva/jwt-go"
	"github.com/labstack/echo"
	"gopkg.in/mgo.v2/bson"
	// "github.com/jinzhu/gorm"
	
	"oxy_server/module/log"
	"oxy_server/model"
	"oxy_server/until"
	
)

type (
	returngoods struct {
		ID bson.ObjectId `json:"_id" bson:"_id"`
		ReturnOrdernumber 	string `json:"returnordernumber" bson:"returnordernumber"`
		Ordernumber 		string `json:"ordernumber" bson:"ordernumber"`
		OnlineOrderNumber 	string `json:"onlineordernumber" bson:"onlineordernumber"`
		ReturnType 			string `json:"returntype" bson:"returntype"`
		CargoName 			string `json:"cargoname" bson:"cargoname"`
		LargeNumber 		string `json:"largenumber" bson:"largenumber"`
		Sku 				string `json:"sku" bson:"sku"`
		Color 				string `json:"color" bson:"color"`
		Size 				string `json:"size" bson:"size"`
		Count 				string `json:"count" bson:"count"`
		ReturnCount 		string `json:"returncount" bson:"returncount"`
		PayDate 			int64 `json:"paydate" bson:"paydate"`
		ShipDate 			int64 `json:"shipdate" bson:"shipdate"`
	}
	insertreturngoods struct {
		ReturnOrdernumber 	string `json:"returnordernumber" bson:"returnordernumber"`
		Ordernumber 		string `json:"ordernumber" bson:"ordernumber"`
		OnlineOrderNumber 	string `json:"onlineordernumber" bson:"onlineordernumber"`
		ReturnType 			string `json:"returntype" bson:"returntype"`
		CargoName 			string `json:"cargoname" bson:"cargoname"`
		LargeNumber 		string `json:"largenumber" bson:"largenumber"`
		Sku 				string `json:"sku" bson:"sku"`
		Color 				string `json:"color" bson:"color"`
		Size 				string `json:"size" bson:"size"`
		Count 				string `json:"count" bson:"count"`
		ReturnCount 		string `json:"returncount" bson:"returncount"`
		PayDate 			int64 `json:"paydate" bson:"paydate"`
		ShipDate 			int64 `json:"shipdate" bson:"shipdate"`
	}
	Size struct{
		S 	int `json:"S"`
		M 	int `json:"M"`
		L 	int `json:"L"`
		XL 	int `json:"XL"`
		XXL int `json:"XXL"`
	}
	Color struct{
		Value  int `json:"value"`
		Size   Size	`json:"size"`
	}
)

type OXYReturnData struct{}

func NewOXYReturnData() *OXYReturnData{
	Data:= &OXYReturnData{}
	return Data
}

func (rd *OXYReturnData) GetReturnDataListHandler(c echo.Context) error {
	currentpage,err := strconv.Atoi(c.QueryParam("currentpage"))
	perpage,err := strconv.Atoi(c.QueryParam("perpage"))
	jsonquery := c.QueryParam("like")
	resultsType := c.QueryParam("type")
	M, err := until.JsonToBson(jsonquery)		
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
			"info": err,
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="return_goods"
	query,count,err:=model.DB().Query(M, currentpage, perpage)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]returngoods{}
	query.All(&ps)
	results := make(map[string]Color)
	if resultsType == "largenumber"{
		for _,item := range ps{
			sizecount,_:=  strconv.Atoi(item.Count )
			_, ok := results[item.Color]
			if !ok {
				results[item.Color] = Color{
					Value: 0,
					Size: Size{
						S: 0,
						M: 0,
						L: 0,
						XL: 0,
						XXL: 0,
					},

				} 
			}
			temp := results[item.Color]
			switch item.Size {
				case "S":
					temp.Size.S += sizecount
					break
				case "M":
					temp.Size.M += sizecount
					break
				case "L":
					temp.Size.L += sizecount
					break
				case "XL":
					temp.Size.XL += sizecount
					break
				case "XXL":
					temp.Size.XXL += sizecount
					break
				
				default:
					break

			}
			temp.Value += sizecount
			results[item.Color] = temp
		}
		c.JSON(200, map[string]interface{}{
			"msg":   "success",
			"data":results,
			"count":count,
		})
	}else{
		c.JSON(200, map[string]interface{}{
			"msg":   "success",
			"data":ps,
			"count":count,
		})
	}
	return nil
}

func (rd *OXYReturnData)AddReturnDataHandler(c echo.Context) error {
	saleslist:= []insertreturngoods{}
	if err := c.Bind(&saleslist); err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
			"info": err,
		})
		return err
	}

	model.DB().Dbname="OxyCms"
	model.DB().Document="return_goods"
	errInfo := []error{}

	for _,item := range saleslist{
		err :=model.DB().Insert(item)
		if err != nil {
			log.Error("更新失败",err)
		errInfo = append(errInfo, err)
		
		}
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"info": errInfo,
	})

	return nil	
}
func (rd *OXYReturnData)GetReturnLargeNumberListHandler(c echo.Context) error{
	jsonquery := c.QueryParam("like")
	M, err := until.JsonToBson(jsonquery)		
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
			"info": err,
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="return_goods"
	query,_,err:=model.DB().Query(M, 0, 0)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]returngoods{}
	query.All(&ps)
	largenumberList :=make([]string,0)
	for _,item := range ps{
		largenumberList =append(largenumberList,item.LargeNumber)
	}
	largenumberList = until.RemoveRepByMap(largenumberList)
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
			"info": err,
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":largenumberList,
		// "count":count,
	})

	return nil
}
func (rd *OXYReturnData)GetReturnSkuListHandler(c echo.Context) error{
	jsonquery := c.QueryParam("like")
	M, err := until.JsonToBson(jsonquery)		
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
			"info": err,
		})
		return err		
	}
	model.DB().Dbname="OxyCms"
	model.DB().Document="return_goods"
	query,_,err:=model.DB().Query(M, 0, 0)
	if err != nil {
		log.Error("查询出错",err)
		c.JSON(404, map[string]interface{}{
			"msg":   "error",
		})
		return err		
	}
	ps:=[]returngoods{}
	skuList:=[]returngoods{}
	query.All(&ps)
	tempMap := map[string]byte{}  // 存放不重复主键
	for _,item := range ps{
        l := len(tempMap)
        tempMap[item.Sku] = 0
        if len(tempMap) != l{  // 加入map后，map长度变化，则元素不重复
			skuList =append(skuList,item)
        }
	}
	if err != nil {
		log.Error("解析参数错误",err)
		c.JSON(403, map[string]interface{}{
			"msg":   "error",
			"info": err,
		})
		return err		
	}
	c.JSON(200, map[string]interface{}{
		"msg":   "success",
		"data":skuList,
		// "count":count,
	})

	return nil
}